package com.cqu.hospitalsystem.controller;

import com.alibaba.fastjson.JSONObject;
import com.cqu.hospitalsystem.entity.Patient;
import com.cqu.hospitalsystem.entity.Recordtemplate;
import com.cqu.hospitalsystem.entity.Registration;
import com.cqu.hospitalsystem.service.RecordtemplateService;
import com.cqu.hospitalsystem.utils.result.DataResult;
import com.cqu.hospitalsystem.utils.result.code.Code;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("recordtemplate")
@Tag(name = "RecordtemplateController", description = "病历模板管理")
public class RecordtemplateController {
    /**
     * 服务对象
     */
    @Resource
    private RecordtemplateService recordtemplateService;

    @PostMapping("searchTemplate")
    @Operation(summary = "搜索模板", description = "根据模板名称搜索病历模板")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未找到模板")
    })
    public DataResult searchTemplate(@RequestBody @Parameter(description = "病历模板对象") Recordtemplate recordtemplate) {
        return DataResult.successByDataArray(this.recordtemplateService.searchTemplate(recordtemplate));
    }

    @PostMapping("saveTemplate")
    @Operation(summary = "保存模板", description = "保存病历模板")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "保存成功"),
            @ApiResponse(responseCode = "400", description = "保存失败")
    })
    public DataResult saveTemplate(@RequestBody @Parameter(description = "病历模板对象") Recordtemplate recordtemplate) {
        System.out.println("saveTemplate");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("isSuccess", this.recordtemplateService.saveTemplate(recordtemplate));
        System.out.println(jsonObject);
        return DataResult.successByData(jsonObject);
    }

    @GetMapping
    @Operation(summary = "分页查询病历模板", description = "根据筛选条件和分页信息查询病历模板")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "400", description = "查询失败")
    })
    public ResponseEntity<Page<Recordtemplate>> queryByPage(Recordtemplate recordtemplate, PageRequest pageRequest) {
        return ResponseEntity.ok(this.recordtemplateService.queryByPage(recordtemplate, pageRequest));
    }

    @GetMapping("{id}")
    @Operation(summary = "根据ID查询病历模板", description = "根据病历模板ID查询单个病历模板")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "病历模板不存在")
    })
    public ResponseEntity<Recordtemplate> queryById(@PathVariable("id") @Parameter(description = "病历模板ID") Long id) {
        return ResponseEntity.ok(this.recordtemplateService.queryById(id));
    }

    @PostMapping
    @Operation(summary = "新增病历模板", description = "创建新的病历模板")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "201", description = "创建成功"),
            @ApiResponse(responseCode = "400", description = "创建失败")
    })
    public ResponseEntity<Recordtemplate> add(@RequestBody @Parameter(description = "病历模板对象") Recordtemplate recordtemplate) {
        return ResponseEntity.ok(this.recordtemplateService.insert(recordtemplate));
    }

    @PutMapping
    @Operation(summary = "编辑病历模板", description = "更新病历模板信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "更新成功"),
            @ApiResponse(responseCode = "400", description = "更新失败")
    })
    public ResponseEntity<Recordtemplate> edit(@RequestBody @Parameter(description = "病历模板对象") Recordtemplate recordtemplate) {
        return ResponseEntity.ok(this.recordtemplateService.update(recordtemplate));
    }

    @DeleteMapping
    @Operation(summary = "删除病历模板", description = "根据病历模板ID删除病历模板")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "删除成功"),
            @ApiResponse(responseCode = "400", description = "删除失败")
    })
    public ResponseEntity<Boolean> deleteById(@Parameter(description = "病历模板ID") Long id) {
        return ResponseEntity.ok(this.recordtemplateService.deleteById(id));
    }
}